package cn.laixueit.utils;

import cn.laixueit.config.SmsProperties;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SmsUtils {


    @Autowired
    private SmsProperties prop;

    public void sendSms(String phone ,String code) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(prop.getAccessKeyId())
                // 您的AccessKey Secret
                .setAccessKeySecret(prop.getAccessKeySecret());
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        Client client = new Client(config);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(prop.getSignName())
                .setTemplateCode(prop.getVerifyCodeTemplate())
                .setTemplateParam("{\"code\":\""+code+"\"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
    }
}
